<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Magnolia Service Driver Library
 * http://wiki.ma.gnolia.com/Ma.gnolia_API
 *
 * $Id$
 *
 * @package		Service Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class Service_Magnolia_Driver extends Service_Driver
{
	protected $api_url = 'https://ma.gnolia.com/api/mirrord/v1';
	protected $port = 443;

	/**
	 *	__construct
	 * 
	 * @param array $config
	 * @return void
	 * @access public
	 * 
	 */
	public function __construct($config = array())
	{
		$config += Kohana::config('service.magnolia');
	
		parent::__construct($config);
	}

	/**
	 * get_tags
	 * 
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_tags()
	{
		return $this->api_call('/tags/get');
	}

	/**
	 * rename_tag
	 * 
	 * @param string $old
	 * @param string $new
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function rename_tag($old, $new)
	{
		$params = array
		(
			'old'	=> $old,
			'new'	=> $new
		);

		return $this->api_call('/tags/rename?'.http_build_query($params));
	}

	/**
	 * get_update
	 * 
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_update()
	{
		return $this->api_call('/posts/all');
	}

	/**
	 * get_posts
	 * 
	 * @param string $tag
	 * @param integer $dt
	 * @param string $url
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_posts($tag = null, $dt = null, $url = null)
	{
		$params = array
		(
			'tag' => $tag,
			'dt'  => date(DATE_ISO8601, $dt),
			'url' => $url
		);

		return $this->api_call('/posts/get?'.http_build_query($params));
	}

	/**
	 * get_posts_recent
	 * 
	 * @param string $tag
	 * @param integer $count
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_posts_recent($tag = null, $count = 100)
	{
		$params = array
		(
			'tag'	=> $tag,
			'count' => $count
		);

		return $this->api_call('/posts/recent?'.http_build_query($params));
	}

	/**
	 * get_posts_all
	 * 
	 * @param string $tag
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_posts_all($tag = null)
	{
		return $this->api_call('/posts/all?'.http_build_query(array('tag' => $tag)));
	}

	/**
	 * get_posts_dates
	 * 
	 * @param string $tag
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_posts_dates($tag = null)
	{
		return $this->api_call('/posts/dates?'.http_build_query(array('tag' => $tag)));
	}

	/**
	 * add_post
	 * 
	 * @param string $url
	 * @param string $description
	 * @param string $extended
	 * @param mixed $tags
	 * @param integer $dt
	 * @param string $replace
	 * @param string $shared
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function add_post($url, $description, $extended = null, $tags = null, $dt = null, $replace = 'no', $shared = 'yes')
	{
		$params = array
		(
			'url'			=> $url,
			'description'	=> $description,
			'extended'		=> $extended, 
			'tags'			=> self::implode($tags, " "),
			'dt'			=> date(DATE_ISO8601, $dt)
		);

		return $this->api_call('/posts/add?'.http_build_query($params));
	}

	/**
	 * del_post
	 * 
	 * @param string $url
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function del_post($url)
	{
		return $this->api_call('/posts/delete?'.http_build_query(array('url' => $url)));
	}

	/**
	 * api_call
	 * 
	 * @param string $api_url
	 * @return object SimpleXMLElement Object
	 * @access protected
	 * 
	 */
	protected function api_call($api_url) 
	{
		if ($this->last_status_code == '503')
			throw new Kohana_User_Exception('Magnolia Driver', Kohana::lang('service.throttle_error'));

		if ( ! $result = parent::api_call($api_url, true))
			throw new Kohana_User_Exception('Magnolia Driver', Kohana::lang('service.invalid_response', $this->last_api_call(), $this->last_status_code()));

		$xml = simplexml_load_string($result);

		if (false === $xml) 
			throw new Kohana_User_Exception('Magnolia Driver', Kohana::lang('service.xml_parse_error'));

		return $xml;
	}

} //End Magnolia Driver